package com.kx.center.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.kx.center.common.CustomHashMap;
import com.kx.center.entity.DailyCounting;
import com.kx.center.entity.DailyCountingDetail;
import com.kx.center.entity.query.QueryDailyCounting;
import com.kx.center.entity.query.QueryResult;
import com.kx.center.mappers.CountingMapper;
import com.kx.center.service.CountingService;
import com.kx.center.util.SerialNumber;

@Service
public class CountingServiceImpl implements CountingService {
	
	@Autowired
	CountingMapper mapper;

	@Override
	public void insertDailyCounting(DailyCounting entity) {
		mapper.insertDailyCounting(entity);
	}

	@Override
	public void insertDailyCountingDetail(DailyCountingDetail entity) {
		mapper.insertDailyCountingDetail(entity);
	}

	@Override
	public void updateDailyCounting(DailyCounting entity) {
		mapper.updateDailyCounting(entity);
	}

	@Override
	public void updateDailyCountingDetail(DailyCountingDetail entity) {
		mapper.updateDailyCountingDetail(entity);
	}

	@Override
	public DailyCounting findDailyCountingById(Integer id) {
		return mapper.findDailyCountingById(id);
	}

	@Override
	public List<DailyCounting> findDailyCountingByStoreId(Integer storeId) {
		return mapper.findDailyCountingByStoreId(storeId);
	}

	@Override
	public List<DailyCountingDetail> findDailyCountingDetailByDCId(Integer dcId) {
		List<DailyCountingDetail> result = new ArrayList<DailyCountingDetail>();
		List<DailyCountingDetail> list = mapper.findDailyCountingDetailByDCId(dcId);
		
		for(DailyCountingDetail item : list) {
			if(item.getQuantity() > 0 || item.getLoss() > 0)
				result.add(item);
		}
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void saveDailyCounting(Integer storeId, DailyCounting counting, List<HashMap<String, Object>> json) {
		
		// TODO 需要事务
		if(counting != null) {
			// 修改
			Iterator<HashMap<String, Object>> iter = json.iterator();
	
			while (iter.hasNext()) {
				HashMap<String, Object> map = iter.next();
				CustomHashMap<String, Object> item = CustomHashMap.convert(map);
				
				DailyCountingDetail entity = new DailyCountingDetail();
				entity.setDcId(counting.getId());
				entity.setGoodsId(item.getInteger("goods_id"));
				entity.setQuantity(item.getInteger("quantity"));
				entity.setLoss(item.getInteger("loss"));
				entity.setMemo(item.getString("memo"));
				
				updateDailyCountingDetail(entity);
			}
			
			updateDailyCounting(counting);
		} else {
			// 新增
			String sn = SerialNumber.getSN(SerialNumber.DAILY_COUNTING, storeId);
	
			counting = new DailyCounting();
			counting.setSn(sn);
			counting.setStatus(0);
			counting.setStoreId(storeId);
	
			insertDailyCounting(counting);
	
			Integer dcid = counting.getId();
	
			Iterator<HashMap<String, Object>> iter = json.iterator();
	
			while (iter.hasNext()) {
				HashMap<String, Object> map = iter.next();
				CustomHashMap<String, Object> item = CustomHashMap.convert(map);
				DailyCountingDetail entity = new DailyCountingDetail();
				entity.setDcId(dcid);
				entity.setSn(sn);
				entity.setGoodsId(item.getInteger("goods_id"));
				entity.setGoodsSku(item.getString("goods_sku"));
				entity.setMemo(item.getString("memo"));
				entity.setQuantity(item.getInteger("quantity"));
				entity.setLoss(item.getInteger("loss"));
	
				// TODO 数据校验
				insertDailyCountingDetail(entity);
			}
	
			counting.setStatus(1);
			updateDailyCounting(counting);
		}
	}

	@Override
	public void confirm(DailyCounting entity) {
		updateDailyCounting(entity);
	}

	@Override
	public QueryResult queryDailyCounting(QueryDailyCounting query) {
		QueryResult result = new QueryResult();
		result.setPage(query.getPage());
		result.setPageSize(query.getPageSize());
		result.setTotalRecord(mapper.queryDailyCountingCount(query));
		result.setData(mapper.queryDailyCounting(query));
		return result;
	}

	@Override
	public DailyCounting findDailyCountingBySN(String sn) {
		return mapper.findDailyCountingBySN(sn);
	}
	
}
